package edu.xhu.video.controller;

import cn.hutool.core.util.IdUtil;
import edu.xhu.model.behavior.dto.AddCommentDto;
import edu.xhu.model.biliuser.pojos.BiliUser;
import edu.xhu.model.common.dtos.ResponseResult;
import edu.xhu.utils.thread.BiliUserThreadLocalUtil;
import edu.xhu.video.service.VideoCommentService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Api(value = "视频评论Controller层", tags = "视频评论Controller层")
@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping("/bilivideo")
public class VideoCommentController {

    private final VideoCommentService videoCommentService;

    /**
     * 获取评论树列表，每次查十条(10条根评论，并包装好了子评论)
     *
     * @param videoId  对应视频ID
     * @param offset   分页偏移量（已经获取到的评论树的数量）
     * @param sortTpye 排序类型 1 按热度排序 2 按时间排序
     * @return 评论树列表
     */
    @GetMapping("/getRootComments")
    public ResponseResult<?> getCommentTreeByVideoId(@RequestParam("vid") Long videoId,
                                                     @RequestParam("offset") Long offset,
                                                     @RequestParam("type") Integer sortTpye) {
        return videoCommentService.getCommentTreeByVideoId(videoId, offset, sortTpye);
    }

    /**
     * 展开更多回复评论
     *
     * @param id 根评论id
     * @return 完整的一棵包含全部评论的评论树
     */
    @GetMapping("/getMoreSonComments")
    public ResponseResult<?> getMoreSonCommentsByRootId(@RequestParam("id") Long id) {
        return videoCommentService.getMoreSonCommentsByRootId(id);
    }

    /**
     * 发表评论
     *
     * @param videoId  视频id
     * @param rootId   根评论id
     * @param parentId 被回复评论id
     * @param toUserId 被回复者uid
     * @param content  评论内容
     * @return 响应对象
     */
    @PostMapping("/addComment")
    public ResponseResult<?> addComment(
            @RequestParam("vid") Long videoId,
            @RequestParam("root_id") Long rootId,
            @RequestParam("parent_id") Long parentId,
            @RequestParam("to_user_id") Long toUserId,
            @RequestParam("content") String content) {

        System.out.println("videoId=" + videoId);
        System.out.println("rootId=" + rootId);
        System.out.println("parentId=" + parentId);
        System.out.println("toUserId=" + toUserId);
        System.out.println("content=" + content);

        //封装一个dto
        BiliUser user = BiliUserThreadLocalUtil.getUser();
        AddCommentDto addCommentDto = new AddCommentDto();
        addCommentDto.setVideoId(videoId)
                .setRootId(rootId)
                .setParentId(parentId)
                .setUserId(user.getUserId())
                .setToUserId(toUserId)
                .setContent(content);

        return videoCommentService.addComment(addCommentDto);
    }
}
